;; Comprehensive test for R4RS String procedures (section 6.7)
;; This file tests all essential string procedures implemented

;; String predicate
(string? "hello")           ; => #t
(string? "")                ; => #t
(string? 42)                ; => #f
(string? 'symbol)           ; => #f
(string? #\a)               ; => #f
(string? '(1 2 3))          ; => #f

;; String construction
(make-string 0)             ; => ""
(make-string 5)             ; => string of length 5
(make-string 3 #\a)         ; => "aaa"
(make-string 2 #\space)     ; => "  "

(string)                    ; => ""
(string #\a)                ; => "a"
(string #\h #\e #\l #\l #\o) ; => "hello"
(string #\! #\@ #\#)        ; => "!@#"

;; String length
(string-length "")          ; => 0
(string-length "a")         ; => 1
(string-length "hello")     ; => 5
(string-length "hello world") ; => 11

;; String reference and mutation
(string-ref "hello" 0)      ; => #\h
(string-ref "hello" 4)      ; => #\o
(string-ref "world" 2)      ; => #\r

(define test-str "hello")
(string-set! test-str 1 #\a)
test-str                    ; => "hallo"

;; String comparison (case sensitive)
(string=? "hello" "hello")  ; => #t
(string=? "hello" "world")  ; => #f
(string=? "" "")            ; => #t

(string<? "apple" "banana") ; => #t
(string<? "banana" "apple") ; => #f
(string<? "apple" "apple")  ; => #f

(string>? "banana" "apple") ; => #t
(string>? "apple" "banana") ; => #f
(string>? "apple" "apple")  ; => #f

(string<=? "apple" "banana"); => #t
(string<=? "apple" "apple") ; => #t
(string<=? "banana" "apple"); => #f

(string>=? "banana" "apple"); => #t
(string>=? "apple" "apple") ; => #t
(string>=? "apple" "banana"); => #f

;; String comparison (case insensitive)
(string-ci=? "Hello" "HELLO") ; => #t
(string-ci=? "Hello" "World") ; => #f

(string-ci<? "Apple" "BANANA") ; => #t
(string-ci<? "BANANA" "apple") ; => #f

(string-ci>? "BANANA" "apple") ; => #t
(string-ci>? "Apple" "BANANA") ; => #f

(string-ci<=? "Apple" "APPLE") ; => #t
(string-ci<=? "Apple" "BANANA") ; => #t

(string-ci>=? "BANANA" "apple") ; => #t
(string-ci>=? "APPLE" "apple")  ; => #t

;; Substring operations
(substring "hello" 0 5)     ; => "hello"
(substring "hello" 1 4)     ; => "ell"
(substring "hello" 0 1)     ; => "h"
(substring "hello" 2 2)     ; => ""
(substring "hello world" 6 11) ; => "world"

;; String concatenation
(string-append)             ; => ""
(string-append "hello")     ; => "hello"
(string-append "hello" " world") ; => "hello world"
(string-append "a" "b" "c") ; => "abc"
(string-append "" "hello" "") ; => "hello"

;; String-list conversion
(string->list "")           ; => ()
(string->list "a")          ; => (#\a)
(string->list "hello")      ; => (#\h #\e #\l #\l #\o)

(list->string '())          ; => ""
(list->string '(#\a))       ; => "a"
(list->string '(#\h #\e #\l #\l #\o)) ; => "hello"

;; Round trip conversion
(list->string (string->list "test")) ; => "test"
(string->list (list->string '(#\a #\b #\c))) ; => (#\a #\b #\c)

;; String copying
(string-copy "")            ; => ""
(string-copy "hello")       ; => "hello"

;; String filling
(define fill-str (string-copy "hello"))
(string-fill! fill-str #\x)
fill-str                    ; => "xxxxx"

;; Complex operations combining multiple procedures
(string-length (string-append "hello" " " "world")) ; => 11
(string? (make-string 5 #\a)) ; => #t
(string=? (string #\a #\b #\c) "abc") ; => #t
(string-ci=? (string #\H #\E #\L #\L #\O) "hello") ; => #t

;; String construction from characters
(string-ref (make-string 3 #\a) 0) ; => #\a
(string-length (list->string (string->list "test"))) ; => 4

;; Mutation and reference
(define mut-str (string-copy "world"))
(string-set! mut-str 0 #\W)
(string-ref mut-str 0)      ; => #\W
mut-str                     ; => "World"

;; Case-insensitive operations
(string-ci<? "Apple" "BANANA") ; => #t
(string-ci>? "Zebra" "apple")  ; => #t

;; Edge cases
(string-length (string))    ; => 0
(string-append "a" "" "b")  ; => "ab"
(substring "hello" 0 0)     ; => ""
(string=? (string-copy "test") "test") ; => #t

;; Complex string building
(list->string
    (cons #\H
        (cdr (string->list "hello")))) ; => "Hello"

;; String comparison chains
(and (string<? "a" "b") (string<? "b" "c")) ; => #t
(or (string>? "z" "a") (string=? "x" "y"))  ; => #t